package com.dangz.dans.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dangz.dans.model.req.TrainsReq;
import com.dangz.dans.utils.CnToSpell;
import com.dangz.dans.utils.HttpHander;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author dangz
 * @since 2018/1/23 15:55
 */
public class TicketGet {
    private static final Logger logger = LoggerFactory.getLogger(TicketGet.class);
    private static final String POST_URL = "http://trains.ctrip.com/TrainBooking/Ajax/SearchListHandler.ashx?Action=getSearchList";

    public static void main(String[] arg0) {
        TrainsReq trains = new TrainsReq();
        trains.setArrivalCity(CnToSpell.getSpell( "广州", false ));
        trains.setDepartureCity(CnToSpell.getSpell( "大英东", false ));
        trains.setArrivalCityName("广州");
        trains.setDepartureCityName("大英东");
        trains.setDepartureDate("2018-2-2");
        JSONObject obj = new JSONObject();
        obj.put("value", JSON.toJSONString(trains));
        try {
            String str = HttpHander.doPost(POST_URL, obj);
            JSONObject result = JSONObject.parseObject(str);
            JSONArray resultS = JSONObject.parseArray(result.get("TrainItemsList").toString());
            JSONArray seatBookingItemObj = new JSONArray();
            for (Object o : resultS) {
                result = (JSONObject) o;
                logger.debug("result:{}", result.toJSONString());
                logger.info("EndStationName(目的地):{}, EndTime(到站时间):{}, StartStationName(始发站)：{}, TakeTime(运行时间):{}, " +
                                "StratTime(发车时间):{}", result.get("EndStationName"), result.get("EndTime"),
                        result.get("StartStationName"), result.get("TakeTime"), result.get("StratTime"));
                seatBookingItemObj = JSON.parseArray(result.get("SeatBookingItem").toString());
                for (Object seatObj : seatBookingItemObj) {
                    result = (JSONObject) seatObj;
                    logger.info("SeatName:{}, Price(价格):{}, Inventory(余票):{}",
                            result.get("SeatName"), result.get("Price"), result.get("Inventory"));
                }
            }
//            logger.info(result.get("TrainItemsList").toString());
//            logger.info(resultS.toJSONString());
//            TicketGet.getFromAndTo("成都", "大英东", "2018-1-24");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
